package com.guohe3.just.service.impl;

import com.guohe3.just.common.constants.Constants;
import com.guohe3.just.common.enums.CrawTypeEnum;
import com.guohe3.just.common.utils.DateUtil;
import com.guohe3.just.craw.CrawService;
import com.guohe3.just.service.SchoolInfoService;
import com.guohe3.just.vo.SchoolCalendarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 浦希成
 * 2018-12-31
 */
@Service
public class SchoolInfoServiceImpl implements SchoolInfoService {

    @Autowired
    private CrawService crawService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public SchoolCalendarVO getSchoolCalendar(String username, String password) throws Exception {

        DateFormat bf = new SimpleDateFormat("yyyy年MM月dd日");
        DateFormat.getDateInstance(DateFormat.FULL).format(new Date());
        String today = bf.format(new Date());
        String week = DateUtil.getDayOfTheWeek();
        List<String> allYear = getAllYearLIst(username, password);
        String weekNum = redisTemplate.opsForValue().get(Constants.XIAOLI_KEY) + "";
        SchoolCalendarVO schoolCalendarVO = new SchoolCalendarVO();
        schoolCalendarVO.setWeekNum(weekNum);
        schoolCalendarVO.setWeek(week);
        schoolCalendarVO.setAllYear(allYear);
        schoolCalendarVO.setToday(today);
        return schoolCalendarVO;
    }


    private List<String> getAllYearLIst(String username, String password) throws Exception {
        List<String> allYearList;
        try {
            allYearList = crawService.crawAllYearList(username, password, CrawTypeEnum.NORMAL);
        } catch (Exception e) {
            allYearList = null;
        }
        if (allYearList == null) {
            allYearList = crawService.crawAllYearList(username, password, CrawTypeEnum.VPN);
        }
        return allYearList;
    }


}
